/**
 * 公用分页mixin
 */

export default {
  data() {
    return {
      page: {
        total: 0,
        page: 1,
        limit: 25,
        ascs: '',
        descs: '',
        filters: {},
        model: {}
      }
    }
  },
  methods: {
    resetPage() {
      this.page = this.$options.data.call(this).page
    },
    makeSureNotEmpty() {
      this.page.filters = this.page.filters || {}
      this.page.filters.rules = this.page.filters.rules || []
    },
    /**
     *
     * @param {FilterRule} rule
     */
    addRule(rule) {
      this.makeSureNotEmpty()
      this.removeRule(rule.field)
      this.page.filters.rules.push(rule)
    },
    addRules(rules) {
      rules.forEach(value => this.addRule(value))
    },
    clearRule() {
      delete this.page.filters.rules
    },
    removeRule(field) {
      this.makeSureNotEmpty()
      const rules = this.page.filters.rules
      const index = rules.findIndex(value => value.field === field)
      if (index > -1) {
        rules.splice(index, 1)
      }
    }
  }
}
